//
//  CommendViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/28.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "CommendViewController.h"
#import "HX_Macro.h"
#import "HXNetTool.h"
#import "HXCommandTCell.h"
#import "HXTalentModel.h"
#import "HXSelectedTCell.h"
#import "HXMarowModel.h"
#import "HXTopicsModel.h"
#import "HXTopicsSmallModel.h"
#import "HXFacutlyTCell.h"
#import "HXDetailVideoVC.h"
#import "HXMaterialsVC.h"
#define kTabelView @"tabelView"
#define kSelected @"selected"
#define kFacutly @"facutly"
@interface CommendViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tabelView;
/**
 *  talent数据源
 */
@property (nonatomic, strong) NSMutableArray *talent;
/**
 *  marrow数据源
 */
@property (nonatomic, strong) NSMutableArray *marrow;
/**
 *  Topics数据源数组
 */
@property (nonatomic, strong) NSMutableArray *topics;
@end

@implementation CommendViewController
- (NSMutableArray *)topics {
    if (!_topics) {
        _topics = [NSMutableArray array];
    }
    return _topics;
}
- (NSMutableArray *)talent {
    if (!_talent) {
        _talent = [NSMutableArray array];
    }
    return _talent;
}
- (NSMutableArray *)marrow {
    if (!_marrow) {
        _marrow = [NSMutableArray array];
    }
    return _marrow;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    [self getNetModelWith];
    [self creatTabelView];
}
- (void)getNetModelWith {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=ShequRecommend&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *data = results[@"data"];
        NSArray *talent = data[@"shequ_talent"];
        for (NSDictionary *dicTalent in talent) {
            [self.talent addObject:[HXTalentModel ModelWithDic:dicTalent]];
        }
        NSArray *marrow = data[@"shequ_marrow"];
        for (NSDictionary *dicMarrow in marrow) {
            [self.marrow addObject:[HXMarowModel ModelWithDic:dicMarrow]];
        }
        NSArray *topics = data[@"shequ_topics"];
        for (NSDictionary *dicTopics in topics) {
            [self.topics addObject:[HXTopicsModel ModelWithDic:dicTopics]];
        }
        [_tabelView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (void)creatTabelView {
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 110) style:0];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [self.view addSubview:_tabelView];
    _tabelView.separatorStyle = 0;
    [_tabelView registerClass:[HXCommandTCell class] forCellReuseIdentifier:kTabelView];
    [_tabelView registerClass:[HXSelectedTCell class] forCellReuseIdentifier:kSelected];
    [_tabelView registerClass:[HXFacutlyTCell class] forCellReuseIdentifier:kFacutly];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.topics.count + 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HXCommandTCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabelView];
        cell.rows = ^(NSInteger row) {
            UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"警告" message:@"你没有登录账号" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"确认" style:0 handler:^(UIAlertAction * _Nonnull action) {
            }];
            UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"取消" style:0 handler:^(UIAlertAction * _Nonnull action) { 
            }];
            [alerController addAction:actionOne];
            [alerController addAction:actionTwo];
            [self showDetailViewController:alerController sender:nil];
        };
        cell.commandSource = self.talent;
        return cell;
    } else if (indexPath.section == 1) {
        HXSelectedTCell *cell = [tableView dequeueReusableCellWithIdentifier:kSelected];
        cell.items = ^(NSInteger row) {
            HXMarowModel *marow = self.marrow[row];
            HXMaterialsVC *detail = [[HXMaterialsVC alloc] init];
            detail.ID = marow.ID;
            [self.navigationController pushViewController:detail animated:YES];
        };
        cell.marrow = self.marrow;
        return cell;
    } else {
        HXFacutlyTCell *cell = [tableView dequeueReusableCellWithIdentifier:kFacutly];
        HXTopicsModel *topics = self.topics[indexPath.section - 2];
        cell.rows = ^(NSInteger row) {
            HXMaterialsVC *mater = [[HXMaterialsVC alloc] init];
            HXMarowModel *model = topics.data[row];
            mater.ID = model.ID;
            [self.navigationController pushViewController:mater animated:YES];
        };
       cell.topics = topics.data;
        cell.backgroundColor = COLORAUTO;
        return cell;
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        UIView *backView = [[UIView alloc] init];
        backView.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] init];
        NSString *text = @"达人";
        label.text = text;
        label.centerX = backView.centerX;
        label.HX_height = 50;
        label.HX_width = [SelfSizeTools selfSizeHeightWithString:text height:50 Font:[UIFont systemFontOfSize:17]];
        [backView addSubview:label];
        UIImageView *headImage = [[UIImageView alloc] init];
        headImage.HX_x = 20;
        headImage.HX_width = 25;
        headImage.HX_height = 25;
        headImage.image = [UIImage imageNamed:@"daren"];
        headImage.centerY = label.centerY;
        label.HX_x = headImage.HX_x + headImage.HX_width + 10;
        [backView addSubview:headImage];
        UIImageView *image = [[UIImageView alloc] init];
        image.image = [UIImage imageNamed:@"moreSettings"];
        image.HX_x = SCREEN_WIDTH - 35;
        image.HX_width = 30;
        image.HX_height = 30;
        image.centerY = label.centerY;
        image.backgroundColor = [UIColor clearColor];
        [backView addSubview:image];
        return backView;
    } else if (section == 1) {
        UIView *backView = [[UIView alloc] init];
        backView.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] init];
        NSString *text = @"精品作品";
        label.text = text;
        label.centerX = backView.centerX;
        label.HX_height = 50;
        label.HX_width = [SelfSizeTools selfSizeHeightWithString:text height:50 Font:[UIFont systemFontOfSize:17]];
        [backView addSubview:label];
        UIImageView *headImage = [[UIImageView alloc] init];
        headImage.HX_x = 20;
        headImage.HX_width = 25;
        headImage.HX_height = 25;
        headImage.image = [UIImage imageNamed:@"jingpin"];
        headImage.centerY = label.centerY;
        label.HX_x = headImage.HX_x + headImage.HX_width + 10;
        [backView addSubview:headImage];
        UIImageView *image = [[UIImageView alloc] init];
        image.image = [UIImage imageNamed:@"moreSettings"];
        image.HX_x = SCREEN_WIDTH - 35;
        image.HX_width = 30;
        image.HX_height = 30;
        image.centerY = label.centerY;
        image.backgroundColor = [UIColor clearColor];
        [backView addSubview:image];
        return backView;
    } else {
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] init];
    NSString *text = [self.topics[section - 2] topic_name];
    label.text = text;
    label.centerX = backView.centerX;
    label.HX_height = 50;
    label.HX_width = [SelfSizeTools selfSizeHeightWithString:text height:50 Font:[UIFont systemFontOfSize:17]];
    [backView addSubview:label];
    UIImageView *headImage = [[UIImageView alloc] init];
    headImage.HX_x = 20;
    headImage.HX_width = 25;
    headImage.HX_height = 25;
    headImage.image = [UIImage imageNamed:@"biaoqian"];
    headImage.centerY = label.centerY;
    label.HX_x = headImage.HX_x + headImage.HX_width + 10;
    [backView addSubview:headImage];
    UIImageView *image = [[UIImageView alloc] init];
    image.image = [UIImage imageNamed:@"moreSettings"];
    image.HX_x = SCREEN_WIDTH - 35;
    image.HX_width = 30;
    image.HX_height = 30;
    image.centerY = label.centerY;
    image.backgroundColor = [UIColor clearColor];
    [backView addSubview:image];
    return backView;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        return 170;
    } else {
        return 155;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
